package com.company.cloud.uba.modules.app.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.uba.modules.app.service.UserBrowseLogAppService;
import com.company.cloud.uba.modules.base.entity.UserBrowseLog;
import com.company.cloud.uba.modules.base.service.UserBrowseLogService;
import com.company.cloud.uba.modules.cache.models.UserIdMappingModel;
import com.company.cloud.uba.modules.cache.models.UserShareModel;
import com.company.cloud.uba.modules.cache.service.UserIdMappingCacheService;
import com.company.cloud.uba.modules.cache.service.UserShareCacheService;
import com.company.cloud.uba.utils.UbaIdUtils;
import com.company.common.server.config.mq.models.MQUserBrowseLogModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserBrowseLogAppServiceImpl implements UserBrowseLogAppService {

    @Autowired
    private UserBrowseLogService userBrowseLogService;
    @Autowired
    private UserIdMappingCacheService userIdMappingCacheService;
    @Autowired
    private UserShareCacheService userShareCacheService;


    @Override
    public void addUserBrowseLog(MQUserBrowseLogModel params) {
        UserBrowseLog entity=new UserBrowseLog();
        entity.setId(UbaIdUtils.getUserBrowseLogId());
        entity.setUserId(params.getUserId());
        entity.setUserToken(params.getUserToken());
        entity.setAnonyToken(params.getAnonyToken());
        entity.setReferralCode(params.getReferralCode());
        entity.setMethodUrl(params.getMethodUrl());
        entity.setMethodType(params.getMethodType());
        entity.setMethodParam(params.getMethodParam());
        entity.setSourceUrl(params.getSourceUrl());
        entity.setClientUa(params.getClientUa());
        entity.setClientType(params.getClientType());
        entity.setMobileNo(params.getMobileNo());
        entity.setLlType(params.getLlType());
        entity.setLongitude(params.getLongitude());
        entity.setLatitude(params.getLatitude());
        entity.setCman(params.getUserId());
        entity.setCdt(params.getOpDate());
        entity.setCip(params.getOpIp());
        entity.setEman(params.getUserId());
        entity.setEdt(LocalDateTime.now());
        entity.setEip(params.getOpIp());

        if(CheckUtils.isEmpty(entity.getUserId()) && StrUtil.isNotEmpty(entity.getAnonyToken())){
            UserIdMappingModel userIdMappingModel=userIdMappingCacheService.getUserIdMappingByCache(entity.getAnonyToken());
            if(userIdMappingModel!=null){
                entity.setUserId(userIdMappingModel.getUserId());
                entity.setUserToken(userIdMappingModel.getLoginToken());
            }
        }

        if(StrUtil.isNotEmpty(entity.getReferralCode())){
//            UserShareModel userShareModel=userShareCacheService.getUserShareByCache(entity.getReferralCode());
        }


        userBrowseLogService.save(entity);
    }


}
